﻿/*
 */
var taskid = 140002;
var changeJobMap = 910000000;
var mX = 300, mY = 120;
var monsterID0 = 9601042;
var monsterID1 = 9601043;
var monsterID2 = 9601044;
var monsterID3 = 9601045;
var monsterID4 = 9601046;
var monsterID5 = 9601042;
var returnMap;
var expInformation;
var cp;
expInformation = 20000;

var dropItems = new Array(
	// - 怪物ID 道具ID 掉率 最小掉落 最大掉落
	new Array(8880101, 4001839, 100, 1, 1) //星星
	// new Array(8880101, 5062000, 4, 1, 1),  //一般神奇魔方
	// new Array(8880101, 5062002, 1, 1, 1),   //高级神奇魔方
	// new Array(8880101, 5062009, 1, 1, 1),   //超级神奇魔方
	// new Array(8880101, 5062010, 1, 1, 1),   //终极神奇魔方
	// new Array(8880101, 5062001, 1, 1, 1),   //混沌神奇魔方
	// new Array(8880101, 5062500, 1, 1, 1)    //大师附加魔方
);


function init() {
	returnMap = em.getMapFactory().getMap(changeJobMap);
}


function setup() {

}


function playerEntry(eim, player) {
	eim.setProperty("finsh", "0");
	eim.setObjectProperty("cp", 0);
	eim.schedule("spawnMonster", 1000, player);
}

function spawnMonster(eim, player) {
	if (eim == null || player == null) {
		return;
	}
	expInformation = 1;
	// if (cp < 200) {
	// 	expInformation = 80000;
	// } else if (cp > 200 && cp < 500) {
	// 	expInformation = 100000;
	// } else if (cp > 500 && cp < 1000) {
	// 	expInformation = 120000;
	// } else if (cp > 1000 && cp < 2000) {
	// 	expInformation = 150000;
	// } else if (cp > 2000 && cp < 8000) {
	// 	expInformation = 200000;
	// }
	var player = eim.getPlayers().get(0);
	var overrideStats = em.newMonsterStats();
	if (eim.getMobs().size() <= 2000) {
		if (player != null) {
			var monster = em.getMonster(monsterID0);
			//monster.disableDrops();
			eim.registerMonster(monster);
			overrideStats.setOHp(12090);
			overrideStats.setOExp(expInformation);
			monster.setOverrideStats(overrideStats);
			em.spawnMonsterOnGroundBelow(player.getMap(), monster, (Math.floor(Math.random() * (mX * 2)) - mX), mY);
		}
		if (player != null) {
			var monster = em.getMonster(monsterID1);
			//monster.disableDrops();
			eim.registerMonster(monster);
			overrideStats.setOHp(12090);
			overrideStats.setOExp(expInformation);
			monster.setOverrideStats(overrideStats);
			em.spawnMonsterOnGroundBelow(player.getMap(), monster, (Math.floor(Math.random() * (mX * 2)) - mX), mY);
		}
		if (player != null) {

			var monster = em.getMonster(monsterID2);
			//monster.disableDrops();
			eim.registerMonster(monster);
			overrideStats.setOHp(12090);
			overrideStats.setOExp(expInformation);
			monster.setOverrideStats(overrideStats);
			em.spawnMonsterOnGroundBelow(player.getMap(), monster, (Math.floor(Math.random() * (mX * 2)) - mX), mY);
		}
		if (player != null) {

			var monster = em.getMonster(monsterID3);
			//monster.disableDrops();
			eim.registerMonster(monster);
			overrideStats.setOHp(12090);
			overrideStats.setOExp(expInformation);
			monster.setOverrideStats(overrideStats);
			em.spawnMonsterOnGroundBelow(player.getMap(), monster, (Math.floor(Math.random() * (mX * 2)) - mX), mY);

		}
		// if (eim.getProperty("finsh") != "1") {
		// 	eim.schedule("spawnMonster", 1000, player);
		// }
	}
}

function playerRevive(eim, player) {
	player.setStance(0);
	player.changeMap(player.getMap(), player.getMap().getPortal(0));
	return true;
}

function scheduledTimeout(eim) {
	var player = eim.getPlayers().get(0);
	eim.setProperty("finsh", "1");
	eim.stopEventClock();
	player.getMap().killAllMonsters(true);
	pauseTimer(eim, player);
	player.changeMap(returnMap, returnMap.getPortal(0));
	//player.openNpc(9310475, 5);
}

function changedMap(eim, player, mapid) {
	if (mapid != changeJobMap) {
		return;
	}
	pauseTimer(eim, player);
}

function playerExit(eim, player) {
	pauseTimer(eim, player);
	player.changeMap(returnMap, returnMap.getPortal(0));
}

function playerDisconnected(eim, player) {
	playerExit(eim, player);
	return 0;
}

function pauseTimer(eim, player) {
	var cp = eim.getObjectProperty("cp");
	em.insertRanking(player, "星星副本", cp);
	eim.unregisterPlayer(player);
	eim.dispose();
}

function playerDead(eim, player) {}

function monsterValue(eim, mobid) {
	cp = eim.getObjectProperty("cp") + 1;
	eim.setObjectProperty("cp", cp);
	eim.broadcastPlayerMsg(-1, "小绵羊：阿 这是第" + cp + " 只被宰杀的小绵羊了");
	return 0;
}

function monsterKilled(eim, player, cp) {}

function allMonstersDead(eim) {
	if (eim.getProperty("finsh") != "1") {
		// eim.schedule("spawnMonster", 1000, player);
		var player = eim.getPlayers().get(0);
		spawnMonster(eim, player);
	}
}

function monsterDamaged(eim, player, mobid, damage) {}

function leftParty(eim, player) {}

function disbandParty(eim) {}

function onMapLoad(eim, player) {}

function cancelSchedule() {}

function pickUpItem(eim, player, itemID) {}

function monsterDrop(eim, player, mob) {
	var toDrop = new Array();
	for (var i = 0; i < dropItems.length; i++) {
		var chance = Math.floor(Math.random() * 100);
		if (chance < dropItems[i][2]) {
			var minQuantity = dropItems[i][3];
			var maxQuantity = dropItems[i][4];
			var quantity = Math.floor(Math.random() * (maxQuantity - minQuantity + 1) + minQuantity);
			toDrop.push(new Array(dropItems[i][1], quantity)); //载入暴率数组
		}
	}
	for (var i = 0; i < toDrop.length; i++) {
		if (player.getMap() != null) {
			player.getMap().spawnMobDrop(em.newItem(toDrop[i][0], 0, toDrop[i][1]), new java.awt.Point(mob.getTruePosition().getX() + 25 * i, mob.getTruePosition().getY()), mob, player, 2, 0);
		}
	}
}
